package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.ProductSaveDto;
import com.tulingxueyuan.mall.dto.ProductUpdateInitDto;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author SiXiaoLong
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    PmsProductService pmsProductService;
    /**
     * 分页 带查询参数
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(value = "verifyStatus",required = false) Integer verifyStatus,
                             @RequestParam(value = "publishStatus",required = false) Integer publishStatus,
                             @RequestParam(value = "brandId",required = false) Long brandId,
                             @RequestParam(value = "productSn",required = false) String productSn,
                             @RequestParam(value = "keyword",required = false) String keyword,
                             @RequestParam(value = "productCategoryId",required = false) Long productCategoryId,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize){
//        Page<PmsProduct> page = new Page<>(pageNum,pageSize);
//        pmsProductService.page(page);
//        if (pageNum > page.getPages()){
//            page = new Page<>((int)page.getPages(),pageSize);
//            pmsProductService.page(page);
//        }
       Page page = pmsProductService.getPage(verifyStatus,publishStatus,brandId,productSn,keyword,productCategoryId,pageNum,pageSize);
       if (pageNum > page.getPages()){
           page = pmsProductService.getPage(verifyStatus,publishStatus,brandId,productSn,keyword,productCategoryId,(int) page.getPages(),pageSize);
       }
//        List<PmsProduct> list = pmsProductService.list();
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 逻辑删除
     */
    @PostMapping("/update/deleteStatus")
    public CommonResult updateDelete(@RequestBody List<Long> ids){
        boolean b = pmsProductService.removeByIds(ids);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("删除失败");
        }
    }

    /**
            * 商品批量上下架
     */
    @PostMapping("/update/publishStatus")
    public CommonResult publishStatus(@RequestBody Map map){
//        根据map.get("publishStatus").toString有值说明是更新  上下架
       boolean b = pmsProductService.updateStatus(map);
       if (b){
           return CommonResult.success(b);
       }else {
           return CommonResult.failed("修改失败");
       }
    }

    /**
     * 是否设为新品
     */
    @PostMapping("/update/newStatus")
    public CommonResult newStatus(@RequestBody Map map){
//        根据map.get("publishStatus").toString有值说明是更新  上下架
        boolean b = pmsProductService.updateStatus(map);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 是否设为推荐
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult recommendStatus(@RequestBody Map map){
//        根据map.get("publishStatus").toString有值说明是更新  上下架
        boolean b = pmsProductService.updateStatus(map);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 商品添加
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody ProductSaveDto productSaveDto){
        boolean b = pmsProductService.create(productSaveDto);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("保存成功");
        }
    }

    /**
     *  商品修改根据id获取商品信息
     */
    @GetMapping("/updateInfo/{id}")
    public CommonResult get(@PathVariable Long id){
        ProductUpdateInitDto pmsProductServiceById = pmsProductService.get(id);
            return CommonResult.success(pmsProductServiceById);
    }

    /**
     * 商品修改
     */
    @PostMapping("/update/{id}")
    public CommonResult updateProduct(@PathVariable Long id,@RequestBody @Valid ProductSaveDto productSaveDto){
        boolean b = pmsProductService.updateProduct(id,productSaveDto);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed("保存成功");
        }
    }
}

